$(function() {
	var loading = false;
	var maxItems = 20;
	var pageSize = 3;
	var listUrl = '/o2o/frontend/listproductsbyshop';
	var pageNum = 1;
	var shop = {};
	var productCategory = {};
	shop.shopId = getQueryString('shopId');
	productCategory.productCategoryId = '';
	var productName = '';
	
	var searchDivUrl = '/o2o/frontend/listshopdetailpageinfo?shopId=' + shop.shopId;
	getSearchDivData();
	addItems(pageSize, pageNum);
	$('#exchangelist').attr('href', 'o2o/frontend/awardlist?shopId=' + shop.shopId);
	function getSearchDivData() {
		var url = searchDivUrl;
		$.getJSON(url, function(data) {
			if(data.success) {
				var shop = data.shop;
				$('#shop-cover-pic').attr('src', shop.shopImg);
				$('#shop-update-time').html(
						new Date(shop.lastEditTime)
						  .Format("yyyy-MM-dd"));
				$('#shop-name').html(shop.shopName);
				$('#shop-desc').html(shop.shopDesc);
				$('#shop-addr').html(shop.shopAddr);
				$('#shop-phone').html(shop.phone);
				
				var productCategoryList = data.productCategoryList;
				var html = '';
				productCategoryList.map(function(item, index) {
					html += '<a href="#" class="button col-33" data-product-category-id="'
						 + item.productCategoryId
						 + '">'
						 + item.productCategoryName
						 + '</a>';
				});
				$('#shopdetail-button-div').html(html);
			}
		});
	}
	
	function addItems(pageSize, pageIndex) {
		var url = listUrl + '?' + 'pageIndex=' + pageIndex + '&pageSize=' + pageSize;
		url += '&productName=' + productName;
		if(productCategory.productCategoryId) {
			url += '&productCategory.productCategoryId=' + productCategory.productCategoryId;
		}
		if(shop.shopId) {
			url += '&shop.shopId=' + shop.shopId;
		}
		$.getJSON(url, function(data) {
			if(data.success) {
				maxItems = data.count;
				var html = '';
				data.productList.map(function(item, index) {
					html += '<div class="card" data-product-id="'
						 + item.productId + '">' +	'<div class="card-header">'
						 + item.productName + '</div>'
						 + '<div class="card-content">'
						 + '<div class="list-block media-list">' + '<ul>'
						 + '<li class="item-content">'
						 + '<div class="item-media">' + '<img src="'
						 + item.imgAddr + '" width="44">' + '</div>'
						 + '<div class="item-inner">'
						 + '<div class="item-subtitle">' + item.productDesc
						 + '</div>' + '</div>' + '</li>' + '</ul>'
						 + '</div>' + '</div>' + '<div class="card-footer">'
						 + '<p class="color-gray">'
						 + new Date(item.lastEditTime).Format("yyyy-MM-dd")
						 + '更新</p>' + '<span>点击查看</span>' + '</div>'
						 + '</div>';
				});
				$('.list-div').append(html);
				var total = $('.list-div .card').length;
				if(total >= maxItems) {
					$('.infinite-scroll-preloader').hide();
				} else {
					$('.infinite-scroll-preloader').show();
				}
				pageNum += 1;
				loading = false;
				$.refreshScroller();
			}
		});
	}
	
	$(document).on('inifinite', '.inifinite-scroll-bottom', function() {
		if(loading)
			return;
		addItems(pageSize, pageNum);
	});
	
	$('#shopdetail-button-div').on('click', '.button', function(e) {
		productCategory.productCategoryId = e.target.dataset.productCategoryId;
		if(productCategory.productCategoryId) {
			if($(e.target).hasClass('button-fill')) {
				$(e.target.removeClass('button-fill'));
				productCategory.productCategoryId = '';
			} else {
				$(e.target).addClass('button-fill').siblings()
				   .removeClass('button-fill');
			}
			$('.list-div').empty();
			pageNum = 1;
			addItems(pageSize, pageNum);
		}
	});
	
	$('#search').on('change', function(e) {
		productName = e.target.value;
		$('.list-div').empty();
		pageNum = 1;
		addItems(pageSize, pageNum);
	});
	
	$('#me').click(function() {
		$.openPanel('#panel-right-demo');
	});
	
	$.init();
});